home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Portable Patmos 1.1 / patmos-src / src / chdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-19  |  2.4 KB  |  131 lines  |  [TEXT/KAHL]

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/stat.h>
  5. #include <sys/errno.h>
  6. #include "crtlocal.h"
  7. #include <Aliases.h>
  8.  
  9. long root_parID;
  10.  
  11. FSSpec getparent(long parID)    
  12.     {
  13.     FSSpec    name;
  14.     OSErr err;
  15.     CInfoPBRec  cPB;
  16.     cPB.dirInfo.ioDrParID = parID;
  17.     do
  18.         {
  19.         /* get information about dir */
  20.         cPB.hFileInfo.ioCompletion = (ProcPtr)0L;
  21.         cPB.hFileInfo.ioNamePtr = (StringPtr)name.name;
  22.         cPB.hFileInfo.ioVRefNum = crt_ioVRefNum;
  23.         cPB.hFileInfo.ioFDirIndex = -1;
  24.         cPB.hFileInfo.ioDirID = cPB.dirInfo.ioDrParID;
  25.  
  26.         err = PBGetCatInfoSync(&cPB); 
  27.         }
  28.     while (0);
  29.     if (err) *name.name = 0;
  30.     name.parID = cPB.dirInfo.ioDrParID;
  31.     name.vRefNum = crt_ioVRefNum;
  32.     return name;
  33.     }
  34.  
  35. FSSpec hfs_canon(long crt_parID, const char *nam, int follow)
  36.     {
  37.     char    *nxt;
  38.     FSSpec    canon;
  39.     long parID;
  40.     char name[255];
  41.     int err;
  42.     Boolean    targetIsFolder,wasAliased;
  43.     struct stat statbuf;
  44.     strcpy(name, nam);
  45.     if (*name=='/') 
  46.         {
  47.         parID = root_parID;
  48.         strcpy(name, &name[1]);
  49.         if (!*name) return getparent(root_parID);
  50.         }
  51.     else
  52.         {
  53.         parID = crt_parID;
  54.         if (!*name)
  55.             {
  56.             *canon.name = 0;
  57.             return canon;
  58.             }
  59.         }
  60.     do
  61.         {
  62.         nxt = strchr(name, '/');
  63.         if (nxt)
  64.             {
  65.             FSSpec canon;
  66.             *nxt = 0;
  67.             canon = hfs_canon(parID, name, 1);
  68.             err = macstat(canon.name, &statbuf, canon.vRefNum, canon.parID);
  69.             if (err)
  70.                 {
  71.                 *canon.name = 0;
  72.                 return canon;
  73.                 }
  74.             if (S_ISDIR(statbuf.st_mode))
  75.                 {
  76.                 parID = statbuf.st_ino;
  77.                 }
  78.             else
  79.                 {
  80.                 errno = ENOTDIR;
  81.                 *canon.name = 0;
  82.                 return canon;
  83.                 }
  84.             strcpy(name, ++nxt);
  85.             }
  86.         }
  87.     while (nxt);
  88.     if (!*name) return getparent(parID);
  89.     if (*name == '.')
  90.         {
  91.         if (!name[1])
  92.             return getparent(parID);
  93.         if ((name[1]=='.') && !name[2])
  94.             {
  95.             canon = getparent(parID);
  96.             return getparent(canon.parID);
  97.             }
  98.         }
  99.     *canon.name = strlen(name);
  100.     BlockMove(name, &canon.name[1], *canon.name);
  101.     canon.parID = parID;
  102.     canon.vRefNum = crt_ioVRefNum;
  103.     if (follow) err = ResolveAliasFile(&canon,true,&targetIsFolder,&wasAliased);
  104.     return canon;
  105.     }
  106.     
  107. int chdir(char *name)
  108.     {
  109.     struct stat statbuf;
  110.     int err;
  111.     if (!*name) return 0;
  112.     err = stat(name, &statbuf);
  113.     if (S_ISDIR(statbuf.st_mode))
  114.         {
  115.         crt_parID = statbuf.st_ino;
  116.         return 0;
  117.         }
  118.     errno = ENOTDIR;
  119.     return -1;
  120.     }
  121.  
  122. int fchdir(int fd)
  123.     {
  124.     if (crt_fd_tab[fd].flags & O_CATALOG)
  125.         {
  126.         crt_parID = crt_fd_tab[fd].fd;
  127.         return 0;
  128.         }
  129.     return -1;
  130.     }
  131.